﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSD.OneStopService.BLL;

public partial class Modules_ChoXuly : System.Web.UI.Page
{
    private ProfileStoreRepository ProfStore;
    private DepartmentRepository DeptRepos;
    private List<ProfileStoreRepository.WaitForProcessProfileInfo> ListProfiles;
    private List<Department> AllDepartment;
    private bool IsManager = false;
    private bool IsDirector = false;
    private bool IsViewAll = false;
    private int CurrentDeptID = -1;

    public int start = 0;
    int pageSize = 15;
    int pageSegSize = 1;
    int count = 0;

    public string searchAllProf
    {
        set;
        get;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ContextUser.PermissionCollection != null)
        {
            IsManager = ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON);
            IsDirector = ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN);
            IsViewAll = ContextUser.PermissionCollection.HasPermission(PermissionCode.XEM_TOANBO_HOSO);
        }
        if (ContextUser.CurrentUser != null && ContextUser.CurrentUser.DepartmentID.HasValue)
            CurrentDeptID = ContextUser.CurrentUser.DepartmentID.Value;
        else CurrentDeptID = -1;
        bool EnabledViewAll = ContextUser.GlobalOption.EnabledViewAll.HasValue && ContextUser.GlobalOption.EnabledViewAll.Value;
        //CbStaffs.Visible = (IsManager || IsDirector || EnabledViewAll || IsViewAll);

        txtSearchProfile.Focus();

        if (!IsPostBack)
        {
            BindProfilesDropdownlist();

        }
    }

    public void Page_PreRender(object sender, EventArgs e)
    {
        //if (CbStaffs.SelectedItem != null)
        //{
        //    Page.Response.Write("Selected:" + CbStaffs.SelectedItem.Value.ToString());
        //    Page.Response.Write("textbox:" + txtSearchProfile.Text);
        //}
        FilterData();
    }

    public void BindProfilesDropdownlist()
    {
        List<Staff> TempList = new List<Staff>();
        int deptid = CurrentDeptID;
        StaffRepository StaffRepos = new StaffRepository();
        if (deptid != 0)
            TempList = StaffRepos.GetStaffByDepartment(deptid);
        else
        {
            List<Staff> allStaff = StaffRepos.GetAll();
            List<Staff> lstNguoiXL = new List<Staff>();
            foreach (Staff s in allStaff)
            {
                PermissionManager pm = new PermissionManager(s.UserName);
                if (pm.HasPermission(PermissionCode.CHUYENVIEN_XULY))
                    lstNguoiXL.Add(s);
            }
            TempList = lstNguoiXL;
        }

        if (!IsDirector)
            TempList.Insert(0, new Staff() { Name = "Toàn bộ", UserName = "" });

        if (IsDirector || IsViewAll)
            TempList.Insert(0, new Staff() { Name = "Toàn bộ", UserName = "" });

        Staff st = TempList.Find(r => r.UserName == ContextUser.UserName);
        if (st == null)
        {
            TempList.Insert(0, new Staff() { Name = "Hồ sơ cá nhân", UserName = ContextUser.UserName });
        }

        if (st != null)
        {
            st.Name = "Hồ sơ cá nhân";
            TempList.Remove(st);
            TempList.Insert(0, st);
        }
        CbStaffs.DataSource = TempList;
        CbStaffs.DataTextField = "Name";
        CbStaffs.DataValueField = "UserName";
        CbStaffs.DataBind();

    }

    public void FilterData()
    {
        try
        {
            int deptid = -1;
            string username = string.Empty;
            if (CbStaffs.SelectedValue.ToString() == "")
            {
                deptid = 0;
                username = string.Empty;
            }
            else
                username = CbStaffs.SelectedValue.ToString().ToLower();

            ProfStore = new ProfileStoreRepository();
            ListProfiles = ProfStore.GetWaitForProcessingProfiles(deptid, username, start, pageSize, out count).ToList();

            searchAllProf = txtSearchProfile.Text.ToString().ToLower();
            if (this.ListProfiles == null) return;
            if (string.IsNullOrWhiteSpace(searchAllProf))
            {
                this.rptHoSo.DataSource = ListProfiles;
                rptHoSo.DataBind();
            }
            else
            {
                ListProfiles = ListProfiles.FindAll(x =>
                    (x.ServiceName != null && x.ServiceName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.SubscriberName != null && x.SubscriberName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.ProfileCode != null && x.ProfileCode.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.DeptName != null && x.DeptName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.Resolver != null && x.Resolver.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.Approver != null && x.Approver.ToLower().Contains(searchAllProf.ToLower()))
                    ).ToList();

                this.rptHoSo.DataSource = ListProfiles;
                rptHoSo.DataBind();
            }
        }
        catch (Exception)
        {
        }
        AspNetPager1.RecordCount = count;
        AspNetPager1.PageSize = pageSize;
        // AspNetPager1.CurrentPageIndex = 1;

        if (AspNetPager1.PageCount <= 1) divPagination.Visible = false;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        txtSearchProfile.Focus();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        start = AspNetPager1.StartRecordIndex - 1;
        try
        {
            int deptid = -1;
            string username = string.Empty;
            if (CbStaffs.SelectedValue.ToString() == "")
            {
                deptid = 0;
                username = string.Empty;
            }
            else
                username = CbStaffs.SelectedValue.ToString().ToLower();

            ProfStore = new ProfileStoreRepository();
            ListProfiles = ProfStore.GetWaitForProcessingProfiles(deptid, username, start, pageSize, out count).ToList();

            searchAllProf = txtSearchProfile.Text.ToString().ToLower();
            if (this.ListProfiles == null) return;
            if (string.IsNullOrWhiteSpace(searchAllProf))
            {
                this.rptHoSo.DataSource = ListProfiles;
                rptHoSo.DataBind();
            }
            else
            {
                ListProfiles = ListProfiles.FindAll(x =>
                    (x.ServiceName != null && x.ServiceName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.SubscriberName != null && x.SubscriberName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.ProfileCode != null && x.ProfileCode.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.DeptName != null && x.DeptName.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.Resolver != null && x.Resolver.ToLower().Contains(searchAllProf.ToLower()))
                    || (x.Approver != null && x.Approver.ToLower().Contains(searchAllProf.ToLower()))
                    ).ToList();

                this.rptHoSo.DataSource = ListProfiles;
                rptHoSo.DataBind();

            }
        }
        catch (Exception)
        {

            throw;
        }

    }
}